package org.example.ruoxing_admin_springboot3.service.user;
import jakarta.annotation.Resource;
import org.example.ruoxing_admin_springboot3.dao.UserInfoDao;
import org.example.ruoxing_admin_springboot3.dao.UserLoginDao;
import org.example.ruoxing_admin_springboot3.dto.UserLoginIframeSSODto;
import org.example.ruoxing_admin_springboot3.exceptionHandleUtils.ExceptionHandleUtils;
import org.example.ruoxing_admin_springboot3.httpResponseResult.ResponseResult;
import org.example.ruoxing_admin_springboot3.mapper.UserInfoMapper;
import org.example.ruoxing_admin_springboot3.utils.JWTUtils;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @description 单点登陆的sso服务
 * @author 莫若省
 * @createTime 2024/6/27
*/
@Service
public class UserLoginSSOService {
    @Resource
    private UserInfoMapper userInfoMapper;
    public ResponseResult<Object> userLogin(UserLoginIframeSSODto userLoginSSODto) {
        try{
            /***
             * TODO: 将 userLoginSSODto 转发给vst 后台进行验证
             *
             */
            //验证通过后根据约的值反查用户，反会资源给前端
            String userNumber = "admin";
            //验证用户信息
            UserLoginDao userLoginDao = new UserLoginDao();
            userLoginDao.setUserNumber(userNumber);
            //根据用户账号查询用户信息
            List<UserInfoDao> userInfoDao = userInfoMapper.getCurrentUserInfoList(userLoginDao);
            if (userInfoDao.isEmpty()) {
                return ResponseResult.WARN("用户账号不存在!");
            }
            //获取用户
            UserInfoDao user = userInfoDao.getFirst();
            //生成token
            JWTUtils.Token tokenMap = JWTUtils.createTokenMap(user);
            user.setUserCurrentToken(tokenMap.getToken());
            //更新用户token
            userInfoMapper.updateUserTokenByUserId(user);
            return  ResponseResult.SUCCESS(tokenMap);
        }catch (Exception e){
            ExceptionHandleUtils.reportException(e, "用户登陆异常!");
            return ResponseResult.ERROR();
        }
    }
}
